package com.physique.tea.Service.Impl;

import com.physique.tea.Global.Result;
//import com.physique.tea.Mapper.QuestionMapper;
import com.physique.tea.Global.ResultList;
import com.physique.tea.Mapper.QuestionMapper;
import com.physique.tea.Pojo.Question;
import com.physique.tea.Service.QuestionService;
import com.physique.tea.dto.QuestionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class QuestionServiceImpl implements QuestionService {


    @Autowired
    QuestionMapper questionMapper;

    @Override
    public Result getAllQuest() {
        List<Question> questions = questionMapper.selectAll();

        return new Result(ResultList.SUCCESS.getCode(),
                ResultList.SUCCESS.getMessage(),
                questions);
    }

    @Override
    public Result getPhysiqueByQuestion(QuestionDto questionDto) {
        if(questionDto.getQuestion().isEmpty()){
            return new Result(ResultList.SUCCESS.getCode(),
                    ResultList.SUCCESS.getMessage(),
                    "平和质");
        }

        //体质问题和相应体质的映射
        HashMap<String,String> questionMapPhysique = new HashMap<>();
//        体质相关问题出现的计数，用户统计是什么体质
        Map<String,Integer> physiqueCount = new TreeMap<>();

        List<Question> baseQuestions = questionMapper.selectAll();
        //创建问题和体质的映射
        for(Question i : baseQuestions){
            String content = i.getCONTENT();
            String physique = i.getPhysique();
            questionMapPhysique.put(content,physique);
        }
        List<String> questions = questionDto.getQuestion();

        for(String s:questions){
            //先取出该问题隶属的体质
            String physique = questionMapPhysique.get(s);
            //给该体质类型得分+1
            physiqueCount.put(physique,physiqueCount.getOrDefault(physique,0)+1);
        }
//        排序得出并返回计分最高的体质类型
        List<Map.Entry<String, Integer>> entries = new ArrayList<>(physiqueCount.entrySet());
        //按照体质的计数值降序排序
        entries.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
        return new Result(ResultList.SUCCESS.getCode(),
                ResultList.SUCCESS.getMessage(),
                entries.get(0).getKey());
    }


}
